import {Component, OnInit} from '@angular/core';
import {MesCreate, MesCreateCondition} from '../../mes-create.type';
import {ActivatedRoute, Router} from '@angular/router';
import {Services} from '../../../../../frame/module/services';
import {ErrRes} from '../../../../../frame/global/err-res';
import {isNullOrUndefined} from 'util';
import {UrlRou} from '../../../../../frame';
import {ModifyMesCreateComponent} from '../modify-mes-create.component';
import {Zero3Alert} from '../../../../../frame/global/zero3-alert/zero3-alert.component';
import {Zero3Enum} from '../../../../../frame/enum/zero3-enum';
import * as fa from '@fortawesome/free-solid-svg-icons';
import {FroalaEditor} from '../../../../../frame/global/froala-editor';
import {SysUserCondition} from '../../../../system-management/sys-user/sys-user.type';
import {MesTypeCondition} from '../../../mes-type/mes-type.type';

@Component({
    selector: 'app-main-modify-mes-create',
    templateUrl: './main-modify-mes-create.component.html',
    styleUrls: ['./main-modify-mes-create.component.css']
})
export class MainModifyMesCreateComponent implements OnInit {

    zero3Enum = Zero3Enum;

    froalaEditorOptions = Object.assign(FroalaEditor.options, {});

    errRes = new ErrRes();

    domain: MesCreate = new MesCreate();

    sendtoname = {};

    selectUserUuid: string[] = [];

    datas = [];

    extend = {
        domain: {
            serAudit: {
                nickname: ''
            },
            file: {
                oss: Services.aliOSS.simple(),
                change: (event) => {
                    console.log(event.target.value); // file name
                    this.extend.domain.file.oss.uploadFile((response, status, headers) => {
                        if (status === 200) {
                            this.extend.domain.file.oss.fileUrl = JSON.parse(response).detail.fileUrl;
                            this.domain.serFileUrl = this.extend.domain.file.oss.fileUrl;
                        } else {
                            this.extend.domain.file.oss.fileUrl = '';
                            this.domain.serFileUrl = this.extend.domain.file.oss.fileUrl;
                            Zero3Alert.req('fail');
                        }
                    });
                }
            },
            serInputGroupData: {
                icon: fa.faCalendar,
                nickname: '日期',
                value: () => {
                    return this.domain.serInputGroupData;
                },
                focus: (event) => {
                    this.domain.serInputGroupData = event.target.value.toString();
                },
                blur: (event) => {
                }
            },
            mesType: {
                items: [],
                icon: fa.faSearchPlus,
                nickname: '消息类型',
                change: (event) => {
                    console.log(event);
                }
            },
            weekType: {
                items: [],
                nickname: '每周几号',
            },
            monthType: {
                items: [],
                nickname: '每月几号',
            },
            serTimePoint: {
                icon: fa.faCalendar,
                nickname: '推送时间',
                value: () => {
                    return this.domain.serTimePoint;
                },
                focus: (event) => {
                    this.domain.serTimePoint = event.target.value.toString();
                },
                blur: (event) => {
                }
            },
            serEffectiveTime: {
                icon: fa.faCalendar,
                nickname: '生效时间',
                value: () => {
                    // return `${this.condition.createTimeOUTSET} - ${this.condition.createTimeFINISH}`;
                    return this.domain.serEffectiveTime;
                },
                focus: (event) => {
                    this.domain.serEffectiveTime = event.target.value;
                },
                blur: (event) => {
                }
            },
        },
    };

    func = {
        loadDomain: (uuid: string) => {
            const condition: MesCreateCondition = new MesCreateCondition();
            condition.uuid = uuid;
            Services.mesCreate.info(condition).subscribe(item => {
                if (!isNullOrUndefined(item) && !isNullOrUndefined(item.domain)) {
                    this.domain = item.domain;
                    this.extend.domain.serAudit.nickname = Zero3Enum.AUDIT_FLAG.get(this.domain.serAudit);
                }
            });
        },
        update: {
            req: (event) => {
                Services.mesCreate.update(this.domain, this.func.update.callback).subscribe();
            },
            callback: {
                success: (result) => {
                    this.router.navigate([UrlRou.mesCreate.list]);
                },
                error: (result) => {
                    this.errRes.error = result.error;
                    this.errRes.statusText = result.statusText;
                }
            }
        },
        close: {
            closeEidt: (editStudentModel: HTMLElement) => {
                editStudentModel.style.visibility = 'hidden';
                this.domain.sendTo = "";
                this.selectUserUuid.forEach(it => {
                    this.domain.sendTo += " " + this.sendtoname[it];
                });
            }
        },
        choose: {
            chooseUser: (editStudentModel: HTMLElement) => {
                Services.sysUser.list(new SysUserCondition(), {
                    page: 0,
                    size: 50,
                    orders: 'createTime-desc,serNum-asc'
                }).subscribe(item => {
                    this.sendtoname = {};
                    this.datas = item.page.content || [];
                    this.datas.forEach(it => {
                        this.sendtoname[it.uuid] = it.serName;
                    });
                });
                editStudentModel.style.visibility = 'visible';
            }
        }
    };

    constructor(public modifyMesCreate: ModifyMesCreateComponent, private router: Router, private route: ActivatedRoute) {
    }

    ngOnInit() {
        this.modifyMesCreate.main = this;
        const uuid = this.route.snapshot.paramMap.get('uuid');
        this.func.loadDomain(uuid);
        Services.mesType.list(new MesTypeCondition(), {
            page: 0,
            size: 999,
            orders: 'createTime-desc,serNum-asc'
        }).subscribe(item => {
            console.log(item);
            if (!isNullOrUndefined(item) && item.code === 'success') {
                this.extend.domain.mesType.items = [];
                item.page.content.forEach(it => {
                    this.extend.domain.mesType.items.push({id: it.uuid, name: it.serName});
                });
            }
        });
        for (let i = 0; i < this.zero3Enum.monthDays.length; i++) {
            this.extend.domain.monthType.items.push({id: (i + 1).toString(), name: this.zero3Enum.monthDays[i]});
        }
        this.extend.domain.weekType.items = [{id: '2', name: '周一'} , {id: '3', name: '周二'} , {id: '4', name: '周三'} , {id: '5', name: '周四'} , {id: '6', name: '周五'} , {id: '7', name: '周六'} , {id: '1', name: '周日'}]
    }

}
